package com.zt.rongda.salary.service;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zt.rongda.salary.inter.SalaryPayRollMapper;
import com.zt.rongda.salary.model.SalaryPayRoll;
import com.zt.servlet.ReqBean;
import com.zt.system.model.BusinessException;
import com.zt.system.model.SqlBuilder;
import com.zt.system.service.BaseService;
import com.zt.util.StringUtil;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

@Service
public class SalaryPayRollService extends BaseService {

	@Autowired
	private SalaryPayRollMapper salaryPayRollMapper;
	@Autowired
	private SalaryPayRollDetailService salaryPayRollDetailService;

	/**
	 * 查询
	 *
	 * @param reqBean
	 * @return
	 */
	public JSONObject getTable(ReqBean reqBean) {
		SqlBuilder sqlBuilder = new SqlBuilder("");
		sqlBuilder.appendSql("select * from SALARY_PAYROLL s where 1=1 ");
		sqlBuilder.addWhere(" s.PAYROLL_YEAR", reqBean.getInfo("payRollYear"));
		sqlBuilder.orderBy(" ORDER BY s.PAYROLL_YEAR desc,s.PAYROLL_MONTH desc");
		return queryTable(sqlBuilder, reqBean.getPageSize(), reqBean.getPageIndex());
	}

	/**
	 * 通过id查询
	 *
	 * @param payrollId
	 * @return
	 */
	public SalaryPayRoll getById(String payrollId) {
		return salaryPayRollMapper.selectByPrimaryKey(payrollId);
	}

	/**
	 * 新增
	 *
	 * @param salaryPayRoll
	 */
	public void add(SalaryPayRoll salaryPayRoll) {
		Date date = new Date();
		SalaryPayRoll payRoll = salaryPayRollMapper.selectByYearAndMonth(salaryPayRoll.getPayrollYear(),
				salaryPayRoll.getPayrollMonth());
		if (payRoll != null) {
			throw new BusinessException("工资单已存在!");
		}
		salaryPayRoll.setPayrollId(stringId());// 手动设置Id
		salaryPayRoll.setStatus("1");
		salaryPayRoll.setMakeTime(date);
		salaryPayRoll.setModifyTime(date);
		salaryPayRollMapper.insert(salaryPayRoll);
		//创建工资单的同时要生成员工单月工资条
		salaryPayRollDetailService.batchCreateSalaryPayRollDetail(salaryPayRoll.getPayrollId());
	}

	/**
	 * 修改
	 *
	 * @param salaryPayRoll
	 */
	public void update(SalaryPayRoll salaryPayRoll) {
		String payrollId = salaryPayRoll.getPayrollId();
		if (StringUtil.isEmpty(payrollId)) {
			throw new BusinessException("获取主键失败");
		}

		salaryPayRoll.setModifyTime(new Date());
		salaryPayRollMapper.updateByPrimaryKeySelective(salaryPayRoll);
	}

	/**
	 * 删除
	 *
	 * @param payrollId
	 */
	public void delete(String payrollId) {
		if (StringUtil.isEmpty(payrollId)) {
			throw new BusinessException("获取主键失败");
		}
		salaryPayRollMapper.deleteByPrimaryKey(payrollId);
	}

	/**
	 * 查询工资单的年份
	 * 
	 * @return
	 */
	public JSONArray getSalaryPayRollYear() {
		SqlBuilder sql = new SqlBuilder(
				"select  DISTINCT sp.PAYROLL_YEAR  from  SALARY_PAYROLL sp order by sp.PAYROLL_YEAR ");
		return this.query(sql);
	}

	/**
	 * 查询工资单年份对应的月份
	 * 
	 * @return
	 */
	public JSONArray getSalaryPayRollMonthByYear(int salaryPayRollYear) {
		SqlBuilder sql = new SqlBuilder(
				"select sp.PAYROLL_MONTH  from  SALARY_PAYROLL sp  where sp.PAYROLL_YEAR= ? ORDER BY sp.PAYROLL_MONTH ",
				salaryPayRollYear);
		return this.query(sql);
	}

}